/**
 * Copyright 2013 Bhushan
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.s4j.sample;

import java.util.Timer;

import org.s4j.signals.core.Bridge;
import org.s4j.signals.core.ConnectionType;

/**
 * Sample application that creates n devices and connects their battery signals to a Beeper. It also creates a random
 * event generator that generates events at regular intervals. Depending on the connections made, appropriate events are
 * triggered.
 * 
 * @author Bhushan
 * 
 */
public class TestMain {

    public static void main(String[] args) throws Exception {
        Device tbl1 = new Device();
        Device tbl2 = new Device();
        Device tbl3 = new Device();
        Device tbl4 = new Device();

        Bridge.connect(tbl1, "LowBattery", Beeper.getInstance(), "StartBeep");
        Bridge.connect(tbl2, "LowBattery", Beeper.getInstance(), "StartBeep");
        Bridge.connect(tbl3, "LowBattery", Beeper.getInstance(), "StartBeep");
        Bridge.connect(tbl4, "LowBattery", Beeper.getInstance(), "StartBeep");

        // Signal with param connected to slot with param
        Bridge.connect(tbl1, "BatteryRecovered", Beeper.getInstance(), "StopBeep");
        Bridge.connect(tbl2, "BatteryRecovered", Beeper.getInstance(), "StopBeep");
        Bridge.connect(tbl3, "BatteryRecovered", Beeper.getInstance(), "StopBeep");
        Bridge.connect(tbl4, "BatteryRecovered", Beeper.getInstance(), "StopBeep");

        UserEventGenerator generator = new UserEventGenerator();
        // Represents a signal-signal connection.
        Bridge.connectSignals(generator, "CascadeTap", ConnectionType.Sync, generator, "TapEvent");
        // Signal with no params connected to a slot with no params
        Bridge.connect(generator, "TapEvent", Beeper.getInstance(), "PauseBeep");

        Device[] tables = new Device[] { tbl1, tbl2, tbl3, tbl4 };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new RandomEventGenerator(tables, generator), 10, 5000);
    }
}
